<template>
  <!-- 行为安全之星集中表彰 -->
  <base-breadcrumb class="safetyStarHonour-container">
    <searchData :searchData="searchArr" @search="handleSearch"></searchData>
    <a-card>
      <div class="table-operator">
          <!-- <title-name title="行为安全之星集中表彰" /> -->
        <a-button v-btn-permission="'hse_028_add_01'" type="primary" @click="handleUpdate('add')" v-if="hideBuquBtn()"
          >新增</a-button
        >
      </div>
      <div class="table-operateors mb20">
        <div class="pub-table-select-count">
          <a-icon type="info-circle" theme="twoTone" />
          <span>
            本年奖励总金额：
            <a>{{ totalInfo.yearTotal || 0 }}</a>
            元
          </span>
          <a-divider type="vertical" />
          <span>
            本月奖励总金额：
            <a>{{ totalInfo.monthTotal || 0 }}</a>
            元
          </span>
        </div>
      </div>
      <list-table
      :loading="loading"
        ref="table"
        size="default"
        rowKey="id"
        :columns="columns"
        :request="loadData"
        :alert="true"
        show-size-changer
        show-quick-jumper
        showPagination="auto"
        :defaultLoad="false"
      >
        <template slot="action" slot-scope="text, record">
          <MoreButton>
          <a @click="handleUpdate('look', record)">查看</a>
          <a
            v-btn-permission="'hse_028_edi_02'"
            @click="handleUpdate('edit', record)"
            v-if="hideBuquBtn()"
            >编辑</a
          >
          <a
            v-btn-permission="'hse_028_del_03'"
            @click="handleUpdate('delete', record)"
            v-if="hideBuquBtn()"
            >删除</a
          >
          </MoreButton>
        </template>
      </list-table>
    </a-card>
  </base-breadcrumb>
</template>

<script>
import SelectSubCompany from '@/components/Select/SelectSubCompany' // 分公司
import SelectProject from '@/components/Select/SelectProject' // 项目
import OrgTreeSelect from '@/components/OrgTreeSelect'
import { getList, deleteData, getTotal } from '@/api/safetySupervision/safetyStar/safetyStarHonour.js'
import Moment from 'moment'

const searchArr = [
  {
    name: '所属单位',
    valueKey: 'searchQuery',
    type: OrgTreeSelect,
  },
  // {
  //   name: '所属分公司',
  //   valueKey: 'branchComId',
  //   type: SelectSubCompany
  // },
  // {
  //   name: '所属项目',
  //   keyValue: 'branchComId',
  //   valueKey: 'projectId',
  //   type: SelectProject
  // },
  {
      name: '月份',
      value: 'month',
      type: 'month',
      setInitValue: true,
      initValue: Moment(new Date()).format('YYYY-MM'),
      attr: {
          allowClear: false,
      }
  }
]
const columns = [
  {
    title: '序号',
    dataIndex: 'index',
    width: 80,
    customRender() {
      return Number(arguments[2]) + 1
    }
  },
  {
    title: '月份',
    ellipsis: true,
    width: 80,
    dataIndex: 'month'
  },
  {
    title: '分公司',
    ellipsis: true,
    width: 180,
    dataIndex: 'branchComName'
  },
  {
    title: '项目',
    ellipsis: true,
    width: 180,
    dataIndex: 'projectName'
  },
  {
    title: '奖励总金额（元）',
    ellipsis: true,
    width: 80,
    dataIndex: 'totalNum'
  },
  {
    title: '发放时间',
    ellipsis: true,
    width: 120,
    dataIndex: 'createTime'
  },
  {
    title: '操作',
    dataIndex: 'action',
    width: 180,
    align: 'center',
    scopedSlots: { customRender: 'action' }
  }
]
export default {
  name: 'safetyStarHonour',
  data() {
    return {
      searchArr: searchArr,
      columns: columns,
      totalInfo: {},
      queryParam: {},
      loading:false
    }
  },
  mounted() {
      this.handleSearch()
  },
  methods: {
    /**
     * 获取统计信息
     */
    async getTotalReq(params) {
      try {
        const res = await getTotal(params)
        this.totalInfo = res.data || {}
      } catch (e) {
        console.error(e)
      }
    },
    loadData(parameter) {
      const params = Object.assign({}, parameter, { query: this.queryParam })
      this.loading = true;
      if(this.queryParam.month){
         this.getTotalReq({ ...params, needCount: false })
         return getList(params).then(res=>{
          this.loading = false;
          return res
         })
      }
    },
    handleSearch(val) {
      this.queryParam = val || {
          month: Moment(new Date()).format('YYYY-MM') // 必须要有月份参数
      }
      this.$refs.table.refresh(true)
    },
    handleUpdate(actionType, data = {}) {
      const commonHandler = () => {
        this.$router.push({
          path: '/safetySupervision/cardNotice/safetyStarHonour/update',
          query: {
            pageType: actionType,
            id: data.id || ''
          }
        })
      }
      const actionMap = {
        add: commonHandler,
        edit: commonHandler,
        look: commonHandler,
        delete: () => {
          this.$confirm({
            content: '请确认是否删除该数据？',
            onOk: async () => {
              const params = {
                id: data.id
              }
              await deleteData(params)
              this.$message.success('删除成功')
              this.handleSearch()
            }
          })
        },
      }
      actionMap[actionType] && actionMap[actionType]()
    }
  }
}
</script>

<style>
</style>
